home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / dev / src / adoc_src.lha / adoc-0.17 / strarg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-20  |  1.9 KB  |  79 lines

  1. /*
  2.  *  STRARG.C
  3.  *
  4.  *  (c)Copyright 1991 by Tobias Ferber,  All Rights Reserved
  5.  *
  6.  *  This file is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published
  8.  *  by the Free Software Foundation; either version 1 of the License,
  9.  *  or (at your option) any later version.
  10.  *
  11.  *  This file is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; see the file COPYING.  If not, write to
  18.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /* $VER: $Id: strarg.c,v 1.4 1995/03/20 18:14:57 tf Exp $ */
  22.  
  23. #include <ctype.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <stdarg.h>
  27.  
  28. /*
  29.  *  strarg() sucht den angebenenen String `key' in der Liste der auf ihn
  30.  *  folgenden Argumente.  Diese Liste muß mit einem "" oder einem (char *)0
  31.  *  enden.  Es wird die Nummer des ersten Arguments zurückgegeben, das
  32.  *  lexikographisch gleich `key' ist.
  33.  *  Das Ergebnis ist 0, wenn `key' nicht in dieser Liste vorkommt.
  34.  */
  35.  
  36. int strarg(char *key, ...)
  37. {
  38.   int result = 0;
  39.   int gotit  = 0;
  40.  
  41.   va_list argp;
  42.   va_start(argp,key);
  43.  
  44.   if(key && *key)
  45.   {
  46.     for(result= gotit= 0; !gotit; result++)
  47.     {
  48.       char *arg= va_arg(argp, char *);
  49.  
  50.       if( arg )
  51.       {
  52.         if( !strcmp(arg,key) )
  53.           gotit= 1;
  54.  
  55.         else if( !(*arg) )
  56.           break;
  57.       }
  58.       else break;
  59.     }
  60.   }
  61.  
  62.   va_end(argp);
  63.   return gotit ? result : 0;
  64. }
  65.  
  66.  
  67. #ifdef TEST
  68. #include <stdio.h>
  69.  
  70. int main(int argc, char *argv[])
  71. {
  72.   if(argc > 1)
  73.     printf( "%d\n", strarg(argv[1], "tobi", "gnu", "foo", "bar", "gnubbel", "42", "blubb", "") );
  74.  
  75.   return 0;
  76. }
  77.  
  78. #endif /*TEST*/
  79.